<template>
    <div class="address">
        <van-address-list
        v-model="chosenAddressId"
        :list="list"
        default-tag-text="默认"
        @add="onAdd"
        @edit="onEdit"
        @select='onSelect'
        />
    </div>
</template>
<script>
import { mapState } from 'vuex'
export default {
    data(){
        return{
            chosenAddressId: '0',
            list: []
        }
    },
    computed:{
        ...mapState(['openid', 'userState']),
    },
    mounted(){
        this.getAddress()
    },
    methods:{
        //选则地址
        async onSelect(item,index){
            console.log(item)
            let form = {
                id:item.id,
                openid:this.openid
            }
            let{data:{code,data}} = await this.$http.post(this.$api.addressdefault,form)
            if (code === 0) {
                this.$router.push('/orderconfirmNow')
            }
        },
        //添加地址
        onAdd() {
            this.$router.push(`/address/detail/none`)
        },
        //修改地址
        onEdit(item, index) {
            this.$router.push(`/address/detail/${item.id}`)
        },
        //获取地址列表
        async getAddress(){
            let form = {
                size:1000,
                current:1,
                openid:this.openid
            }
            let {data:{code,data}} = await this.$http.post(this.$api.addressList,form)
            if (code === 0) {
                data.items.forEach(item=>{

                    if (item.is_default == 0) {
                        this.chosenAddressId = item.id
                        this.list.push({
                            id:item.id,
                            name:item.name,
                            tel:item.mobile,
                            address:item.province+item.city+item.district+item.address,
                            isDefault:true
                        })
                    }else{
                        this.list.push({
                            id:item.id,
                            name:item.name,
                            tel:item.mobile,
                            address:item.province+item.city+item.district+item.address,
                            isDefault:false
                        })
                    }
                })
                console.log(this.list)
            }
        },
    }
}
</script>
<style lang="scss" scoped>

</style>